<template>
  <ve-modal title="修改Env" v-model="visible" cancelText="取消" @close="visible = false" @confirm="onConfirm">
    <ve-form :model="form" ref="formRef">
      <ve-form-item label="环境">
        <ve-radio v-model="form.env" :list="radiolist" @confirm="radioconfirm"></ve-radio>
      </ve-form-item>
      <ve-form-item prop="pwd" label="密码">
        <ve-input v-model="form.pwd" />
      </ve-form-item>
    </ve-form>
  </ve-modal>
</template>

<script setup>
  import { ref } from 'vue'
  import { appConfig, envConfig } from '@/config/index.js'

  const emit = defineEmits(['ok'])
  const visible = ref(false)

  const radiolist = [
    {
      value: 'dev',
      label: 'dev',
    },
    {
      value: 'test',
      label: 'test',
    },
    {
      value: 'test02',
      label: 'test02',
    },
    {
      value: 'pre',
      label: 'pre',
    },
    {
      label: 'prod',
      value: 'prod',
    },
  ]

  const form = ref({})

  const radioconfirm = (option) => {
    form.value.env = option.value
  }

  const open = (record) => {
    const env = getApp().globalData.env
    visible.value = true
    form.value = {
      pwd: '',
      env: env,
    }
  }
  // 点击确认
  const formRef = ref()
  const onConfirm = () => {
    if (form.value.pwd != 'qccx2024') {
      uni.showToast({
        title: '密码错误',
        icon: 'error',
        duration: 2000,
      })
      return
    }
    visible.value = false
    // 配置请求地址
    const tragetEnv = envConfig[form.value.env]
    uni.setStorageSync('env_v', form.value.env)
    getApp().globalData.env = form.value.env
    getApp().globalData.requestUrl = tragetEnv.apiHost + appConfig.apiBaseUrl
    getApp().globalData.requestImgUrl = tragetEnv.imgUrl
    uni.showToast({
      title: '环境切换成功',
      icon: 'success',
      duration: 2000,
    })
    emit('ok')
  }
  defineExpose({
    open,
  })
</script>
<style lang="less" scoped>
  :deep(.radio .radio_class) {
    flex-wrap: wrap;
    justify-content: flex-start;
    .title {
      text-align: right;
      margin-bottom: 16rpx;
      margin-right: 16rpx;
    }
  }
</style>
